#coding=utf-8
__author__ = 'liangdong'
from numpy import *
import random
import math

def UserSimilarity(train):
    W=dict()
    for u in train.keys():
        for v in train.keys():
            if u == v:
                continue
            W.setdefault(u,{})
            W[u][v] = len(set(train[u].keys()) & set(train[v].keys()))
            W[u][v] /= math.sqrt(len(train[u])*len(train[v])*1.0)
    return W